
#ifndef  _RADIX_ARCHAISM_DATASET_H_
#define  _RADIX_ARCHAISM_DATASET_H_

#include <Archaism/Header.h>
#include <deque>
#include <map>

_BeginNamespace(Radix)

#ifndef _RadixDataSetVersion
#define _RadixDataSetVersion 0x00010001
#endif//_RadixDataSetVersion

enum DataType
{
	eDataType_Raw,
	eDataType_Number,
	eDataType_Bool,
	eDataType_String
};
/*
============================================================================================
==== DataCell
============================================================================================
*/
class DataCell
{
public:
	DataCell();
	~DataCell();

public:
	uchar* data() const;
	ushort length() const;
	void setData(uchar* data, ushort length);
	void clear();

public:
	DataCell& operator=(const DataCell& cell);
	DataCell& operator=(const String& value);
	DataCell& operator=(char value);
	DataCell& operator=(uchar value);
	DataCell& operator=(short value);
	DataCell& operator=(ushort value);
	DataCell& operator=(int value);
	DataCell& operator=(uint value);
	DataCell& operator=(long value);
	DataCell& operator=(ulong value);
	DataCell& operator=(longlong value);
	DataCell& operator=(ulonglong value);
	DataCell& operator=(float value);
	DataCell& operator=(double value);
	DataCell& operator=(longdouble value);
	DataCell& operator=(bool value);

	operator String();
	operator char();
	operator uchar();
	operator short();
	operator ushort();
	operator int();
	operator uint();
	operator long();
	operator ulong();
	operator longlong();
	operator ulonglong();
	operator float();
	operator double();
	operator longdouble();
	operator bool();

private:
	ushort mLength;
	uchar* mData;
};
/*
============================================================================================
==== DataCol
============================================================================================
*/
class DataCol
{
	friend class DataSet;

public:
	typedef std::deque<DataCell*> CellDeque;

public:
	DataCol(const String& name);
	~DataCol();

public:
	const String& name() const;
	const String& comm() const;
	ushort length() const;
	const DataType& type() const;

	void setComm(const String& comm);
	void setLength(ushort length);
	void setType(const DataType& type);

	DataCell* createCell();
	DataCell* selectCell(uint rowId);
	void deleteCell(uint rowId);
	void clearCells();

private:
	String mName;
	String mComm;
	DataType mType;
	ushort mLength;
	CellDeque mCells;
};
/*
============================================================================================
==== DataRow
============================================================================================
*/
class DataRow
{
public:
	typedef std::map<String, DataCell*> CellMap;

public:
	DataRow();
	~DataRow();

public:
	DataCell& operator[](const String& colName);

public:
	void setCell(const String& colName, DataCell* cell);
	DataCell* getCell(const String& colName);

private:
	CellMap mCells;
};
/*
============================================================================================
==== DataTable
============================================================================================
*/
class DataTable
{
	friend class DataSet;

public:
	typedef std::map<String, DataCol*> ColMap;

public:
	DataTable(const String& name);
	~DataTable();

public:
	DataCol& operator[](const String& colName);
	DataRow operator[](uint rowId);

public:
	const String& name() const;
	const String& comm() const;
	void setComm(const String& comm);

	uint colCount() const;
	uint rowCount() const;

	bool containsCol(const String& colName);
	DataCol* createCol(const String& colName);
	DataCol* selectCol(const String& colName);
	void deleteCol(const String& colName);

	DataRow createRow();
	DataRow selectRow(uint rowId);
	void	deleteRow(uint rowId);

	void clear();

private:
	String mName;
	String mComm;
	ColMap mCols;
	uint mRowCount;
};
/*
============================================================================================
==== DataSet
============================================================================================
*/
class DataSet
{
public:
	typedef std::map<String, DataTable*> TableMap;

public:
	DataSet();
	~DataSet();

public:
	DataTable& operator[](const String& tableName);

public:
	uint tableCount() const;
	bool containsTable(const String& tableName);
	DataTable* createTable(const String& tableName);
	DataTable* selectTable(const String& tableName);
	void deleteTable(const String& tableName);

	void load(uchar* bytes);
	void save(uchar* bytes, uint* length);
	void clear();

private:
	uint mVersion;
	TableMap mTables;
};

_EndNamespace(Radix)

#endif//_RADIX_ARCHAISM_DATASET_H_
